from rest_framework.viewsets import ModelViewSet
from meiduo_admin.my_paginate import MyPageNumberPagination
from . import sku_serializers
from goods.models import SKU,SPU,SPUSpecification
from rest_framework.generics import ListAPIView
from goods.models import GoodsCategory

#1,skus管理
class SKUViewSet(ModelViewSet):
    pagination_class = MyPageNumberPagination
    # queryset = SKU.objects.all()
    serializer_class = sku_serializers.SKUSerializer

    def get_queryset(self):
        #1,获取关键字
        keyword = self.request.query_params.get("keyword")

        #2,返回数据源
        if keyword:
            return SKU.objects.filter(name__contains=keyword).all()
        else:
            return SKU.objects.all()

#2,sku 分类
class SKUCategoryView(ListAPIView):
    # queryset = GoodsCategory.objects.all()
    serializer_class = sku_serializers.SKUCategorySerializer

    #1, 重写get_qureyset获取三级分类
    def get_queryset(self):
        return GoodsCategory.objects.filter(subs=None).all()

#3,spu 简要信息
class SPUListView(ListAPIView):
    queryset = SPU.objects.all()
    serializer_class = sku_serializers.SPUListSerializer

#4,spu specification 信息
class SPUSpecificationListView(ListAPIView):
    # queryset = SPUSpecification.objects.all()
    serializer_class = sku_serializers.SPUSpecificationSerializer

    def get_queryset(self):
        #1,获取spu_id
        spu_id = self.kwargs.get("spu_id",1)

        #2,返回数据
        return SPUSpecification.objects.filter(spu_id=spu_id).all()